package com.circle.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

/**
* 说明:
* @author 作者 E-mail:shanrenlei@126.com
* @version 创建时间：Apr 15, 2016 3:30:50 PM
*/
public class MyRealm1 implements Realm{

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "myrealm1";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		// TODO Auto-generated method stub
		return token instanceof UsernamePasswordToken;
	}

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = (String)token.getPrincipal(); //得到用户名
		String password = new String((char[])token.getCredentials()); //得到密码
		if(!"zhang".equals(username)) {
		throw new UnknownAccountException(); //如果用户名错误
		}
		if(!"123".equals(password)) {
		throw new IncorrectCredentialsException(); //如果密码错误
		}
		//如果身份认证验证成功,返回一个 AuthenticationInfo 实现;
		return new SimpleAuthenticationInfo(username, password, getName());
	}

}
